/*
 * @(#)AutomaticTaskFactory.java
 *
 * Copyright (c) 2006 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.core;

import java.io.File;

import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.workflow.bean.WorkflowAction;
/**
 * <p>Class Name:    AutomaticTaskFactory.java    </p>
 * @author           tc //mod by greatwall
 * @company          DCIVision Limited
 * @creation date    23/08/2006
 */
public class AutomaticTaskFactory {

  private static WorkflowAutomaticTaskClassLoader hotDeploy=null;
  private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(AutomaticTaskFactory.class.getName());
  private static String srcDir=SystemParameterFactory.getSystemParameter(SystemParameterConstant.WORKFLOW_AUTOMATIC_TASK_SRCDIR);
  
  static{
    hotDeploy = new WorkflowAutomaticTaskClassLoader();
    hotDeploy.addSourceDir(new File(srcDir));
  }
  public static WorkflowAutomaticTaskClassLoader getClassLoader()
  {
    if(hotDeploy==null){
      hotDeploy = new WorkflowAutomaticTaskClassLoader();
      hotDeploy.addSourceDir(new File(srcDir));
    }
      return hotDeploy;
  }
  
  public static WorkflowProcessor getInstance(WorkflowAction action){
    
    if(action.getCreatorID().intValue()>0){
      WorkflowProcessor wfProcessor;
      WorkflowAutomaticTaskClassLoader classloader=AutomaticTaskFactory.getClassLoader();
     try {
        Class clazz=classloader.loadClass(action.getActionClassName());
        if(!AutomaticTaskInterface.class.isAssignableFrom(clazz)){
          return (WorkflowProcessor)clazz.newInstance();          
        }
      } catch (Exception e) {
        throw new AutoTaskCompileException(e.toString());
      }
      AutomaticTaskInterface automatictemp = (AutomaticTaskInterface)classloader.newProxyInstance(AutomaticTaskInterface.class, action.getActionClassName());
      wfProcessor= automatictemp.getWorkflowProgress();
      
      return wfProcessor;
      }else{
        try {
          return (WorkflowProcessor)Class.forName(action.getActionClassName()).newInstance();
        } catch (InstantiationException e) {
          log.error(e.toString());
          return null;
        } catch (IllegalAccessException e) {
          log.error(e.toString());
          return null;
        } catch (ClassNotFoundException e) {
          log.error(e.toString());
          return null;
        }
      }
    }

}
